import base64
import urllib.request
import os
from sys import platform

def unpause_namecoin():
    request = urllib.request.Request("http://127.0.0.1:8336/", data=b'{"id":"stemns","method":"unpausenetwork","params":[]}')
    auth = b"user:password"
    authb64 = base64.b64encode(auth).decode("utf-8")
    request.add_header("Authorization", f"Basic {authb64}")
    urllib.request.urlopen(request)

def exit_namecoin():
    request = urllib.request.Request("http://127.0.0.1:8336/", data=b'{"id":"stemns","method":"daemon","params":[{"subcommand": "stop"}]}')
    auth = b"user:password"
    authb64 = base64.b64encode(auth).decode("utf-8")
    request.add_header("Authorization", f"Basic {authb64}")
    urllib.request.urlopen(request)

    # Can't use sys.exit() here because it's called from a child thread.
    os._exit(0)

stemns_dir = os.path.dirname(os.path.realpath(__file__))
bin_base = stemns_dir + "/.."

ncprop279 = bin_base + "/ncprop279/ncprop279"

_service_to_command = {
    "bit.onion": [ncprop279],
    "bit": [ncprop279],
}

_bootstrap_callback = unpause_namecoin
_exit_callback = exit_namecoin
